from src.perceptron.perceptron import Preceptron as preceptron


class LinearElement(preceptron):
    '''
        线性单元只是在感知器模型的基础上，将原来的激活函数activator 从阶跃函数变成了f(x) = x，
    同时，由于我已经将激活函数activator 与参数的个数 param_num 封装入了 perceptron，所以不需要在封装
    '''
    def __init__(self, param_num):
        super().__init__(param_num, 0.01, acti)


def acti(x):
    return x


if __name__ == '__main__':
    # 构建训练数据
    # 输入向量列表，每一项是工作年限
    input_vecs = [[5], [3], [8], [1.4], [10.1]]
    # 期望的输出列表，月薪，注意要与输入一一对应
    labels = [5500, 2300, 7600, 1800, 11400]

    linearEle = LinearElement(param_num=1)
    linearEle.train(iteration=10, input_vecs=input_vecs, labels=labels)

    print(linearEle.__str__())
    print(
    'Work 3.4 years, monthly salary = %.2f' % linearEle.predict([3.4]))
    print(
    'Work 15 years, monthly salary = %.2f' % linearEle.predict([15]))
    print(
    'Work 1.5 years, monthly salary = %.2f' % linearEle.predict([1.5]))
    print(
    'Work 6.3 years, monthly salary = %.2f' % linearEle.predict([6.3]))